// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
	title: {
		rules: [
			{required: true,errorMessage: '请输入标题',trigger: 'blur'}
		]
	},
	abstract : {
		rules:[{required: true,errorMessage: '请输入公告摘要',trigger: 'blur'}]
	},
	content : {
		rules:[{required: true,errorMessage: '请输入公告内容',trigger: 'blur'}]
	},
	type: {
		rules:[{required: true,errorMessage: '请选择公告类型',trigger: 'blur'}]
	},
}

const enumConverter = {}

function filterToWhere(filter, command) {
	let result ;
	switch (command) {
		case 'type' :
			result = onType(filter)
			break;
	}
	return  result
}
function onType(filter){
	if (filter.length === 0) {
		return [{}]
	}
	return filter.map(item => {
		return { "type.value":item }
	})
}

/**
 * 标签数据
 * @param towshipName {array} 乡镇名称
 * @return {Array} 个体id
*/
async function onTowshipData(towshipName){
	if ( !towshipName ) {
		return { code:500,error:'参数不全',towshipName  }
	}
	const db = uniCloud.database()
	try{
		let array = await Promise.all( towshipName.map(async item=>{
			let value = item.value
			let shopField = "_id,area_all,intro,title,logo,address,create_time,money,create_uid,create_time"
			let authentcationField = "permit_photo"
			let shopData = db.collection("shop").field(shopField).getTemp({getOne: false})
			let authenticationData = db.collection("shop_authentication").field(authentcationField).getTemp({getOne: true})
			
			const res = await db.collection(shopData, authenticationData).where({
				"area_all.value":value,
				"_id.shop_authentication": {$size: 1},
			}).get({getOne: false,getCount: true,customUI: true})
			
			return res.result.data
		}))
		let result = array.flat().map(item => {
			return item._id._value
		})
		
		return {
			code:200,
			result
		}
		
	}catch(error){
		return{
			code:404,
			error
		}
	}
}
/**
 * 个人数据
 * @param personId {array} 个人数据
 * @return {Array} 个人id
*/
async function onPersonUid(personId,appId='__UNI__1EEA680'){
	if ( !personId ) {
		return { code:500,error:'参数不全',personId  }
	}
	const db = uniCloud.database()
	try{
		let result = personId.map(function (item){
			switch (appId) {
				case '__UNI__1EEA680':
					return item._id._value;
					break;
				case '__UNI__8A62930':
					return item._id._value;
					break;
				default :
					return item._id
					break;
			}
		})
		return {
			code:200,
			result
		}
	}catch(error){
		return{
			code:404,
			error
		}
	}
}
// 返回 标签默认值
function onDefault(value,formData){
	if (!formData.screens_uid || !value ) {
		return false
	}
	if (formData.platform ==='指定标签')
		return  formData.label_arr.some(function (element) {
			return value === element.value;
		});
	else if (formData.platform === '单独发送')
		return formData.screens_uid.some(function (element) {
			return value === element;
		});
}

export {
	validator,
	enumConverter,
	filterToWhere,
	onTowshipData,
	onPersonUid,
	onDefault,
}